package com.fc.backingbeans.food;

import javax.faces.bean.ViewScoped
import javax.faces.event.ActionEvent

import org.primefaces.event.ScheduleEntryMoveEvent
import org.primefaces.event.SelectEvent
import org.primefaces.model.DefaultScheduleEvent
import org.primefaces.model.DefaultScheduleModel
import org.primefaces.model.ScheduleEvent
import org.primefaces.model.ScheduleModel
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import com.fc.entities.FoodJournal
import com.fc.entities.Recipe
import com.fc.entities.User
import com.fc.service.FoodJournalService
import com.fc.service.RecipeService
import com.fc.service.UserDetailsService
import com.fc.service.UserService

/**
 * 
 * @author Ács Ádám
 * 
 */
@Component("foodJournalController")
@ViewScoped
class FoodJournalController implements Serializable {
	@Autowired FoodJournalService foodJournalService
	@Autowired RecipeService recipeService
	@Autowired UserDetailsService userDetailsService
	@Autowired UserService userService
	List<FoodJournal> entries
	FoodJournal entry = new FoodJournal()
	ScheduleModel eventModel
	ScheduleEvent event = new DefaultScheduleEvent()
	FoodJournal readonlyEntry = new FoodJournal()
	boolean needRefresh

	/** 
	 * Visszaadja a felhasználó által felvett és az alapértelmezett recepteket
	 */
	List<Recipe> findRecipes() {
		List<Recipe> recipes = []
		recipes += recipeService.findAllByUser(userDetailsService.currentUser)
		recipes += recipeService.findAllByUser(userService.find(User.class, 1))
		return recipes
	}

	ScheduleModel getEventModel() {
		if (eventModel == null || needRefresh) {
			eventModel= new DefaultScheduleModel();
			def entries = foodJournalService.findAllByUser(userDetailsService.currentUser);
			for (entry in entries) {
				eventModel.addEvent(new DefaultScheduleEvent(entry.recipe.name, entry.date, entry.date, entry))
			}
			needRefresh = false;
		}

		return eventModel;
	}

	void create(FoodJournal entry) {
		if (entry != null) {
			entry.user = userDetailsService.currentUser
			foodJournalService.create(entry)
			entry = new FoodJournal()
		}
	}

	void delete(FoodJournal entry) {
		if (entry != null) {
			foodJournalService.delete(entry)
			entry = new FoodJournal()
			needRefresh = true
		}
	}

	void addEvent(ActionEvent actionEvent) {
		if(event.getId() == null) {
			event = new DefaultScheduleEvent(entry.recipe.name, entry.date, entry.date, entry)
			create(entry)
			eventModel.addEvent(event)
		}
		else {
			eventModel.updateEvent(event)
		}

		entry = new FoodJournal()
		event = new DefaultScheduleEvent()
	}

	void onEventSelect(SelectEvent selectEvent) {
		event = (ScheduleEvent) selectEvent.getObject();
		readonlyEntry = (FoodJournal) event.getData()
	}

	void onDateSelect(SelectEvent selectEvent) {
		Date date = (Date) selectEvent.getObject()
		event = new DefaultScheduleEvent("", date, date, false)
		entry.setDate((Date) event.getStartDate())
	}

	void onEventMove(ScheduleEntryMoveEvent event) {
		ScheduleEvent sch = event.getScheduleEvent();
		FoodJournal entry = sch.data;
		Date newStartDate = sch.startDate;
		foodJournalService.save(entry);
		entry = new FoodJournal()
	}
}
